home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / SimpleApp_Sound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  10.1 KB  |  341 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines demonstrating how to play a sound with many of the same features as QuickTime.
  5. **
  6. **    by Mark Cookson, Apple Developer Technical Support
  7. **
  8. **    File:    SimpleApp Sound.c
  9. **
  10. **    Copyright ©1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "Apple Sample
  17. **    Code" after having made changes. If you're going to re-distribute the
  18. **    source, we require that you make it clear in the source that the code
  19. **    was descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include "SimpleApp_Sound.h"
  23.  
  24. /*-----------------------------------------------------------------------*/
  25. pascal    void    MyScrollAction (ControlHandle control, short part)
  26. /*-----------------------------------------------------------------------*/
  27. {
  28.     long    position = GetControlValue (control);
  29.  
  30.     if (part) {
  31.         switch (part) {
  32.         case kInUpButtonControlPart:
  33.             position -= oneBuffer;
  34.             if (CDStyle == true) {
  35.                 if (gPlayBackwards == true) {
  36.                     ASoundPlayBackwards (mySoundInfo, true);
  37.                 }
  38.             }
  39.             break;
  40.         case kInDownButtonControlPart:
  41.             position += oneBuffer;
  42.             break;
  43.         case kInPageUpControlPart:
  44.             position -= tenBuffers;
  45.             if (CDStyle == true) {
  46.                 if (gPlayBackwards == true) {
  47.                     ASoundPlayBackwards (mySoundInfo, true);
  48.                 }
  49.             }
  50.             break;
  51.         case kInPageDownControlPart:
  52.             position += tenBuffers;
  53.             break;
  54.         default:
  55.             DebugStr ("\pWhere did you click?");
  56.         }
  57.         SetControlValue (control, position);
  58.         ASoundSetCurBuffer (mySoundInfo, position);
  59.  
  60.         if (CDStyle == true) {
  61.             ASoundSetBytesCopied (mySoundInfo, position * ASoundGetBufferSize (mySoundInfo));
  62.         }
  63.     }
  64. }
  65.  
  66. /*-----------------------------------------------------------------------*/
  67. pascal    void    DoScroll (ControlHandle control, short part)
  68. /*-----------------------------------------------------------------------*/
  69. {
  70.     long    position    = nil;
  71.     Point    pt            = {nil, nil};
  72.     OSErr    theError    = noErr;
  73.     
  74.     if (part != nil) {
  75.         switch (part) {
  76.             case kInIndicatorControlPart:
  77.                 if (CDStyle == false) {
  78.                     theError = ASoundPauseForAdjust (mySoundInfo);
  79.                 }
  80.                 GetMouse(&pt);
  81.                 part = TrackControl (control, pt, nil);
  82.                 position = GetControlValue (control);
  83.                 ASoundSetCurBuffer (mySoundInfo, position);
  84.                 ASoundSetBytesCopied (mySoundInfo, position * ASoundGetBufferSize (mySoundInfo));
  85.                 break;
  86.             case kInUpButtonControlPart:
  87.             case kInDownButtonControlPart:
  88.             case kInPageUpControlPart:
  89.             case kInPageDownControlPart: {
  90.                 ControlActionUPP actionProc = NewControlActionProc (MyScrollAction);
  91.                 if (CDStyle == false) {
  92.                     theError = ASoundPauseForAdjust (mySoundInfo);
  93.                 }
  94.                 part = TrackControl (control, pt, actionProc);
  95.                 DisposeRoutineDescriptor (actionProc);
  96.                 break;
  97.             }
  98.         }
  99.     
  100.         if (CDStyle == false) {
  101.             position = GetControlValue (control);
  102.             ASoundSetBytesCopied (mySoundInfo, position * ASoundGetBufferSize (mySoundInfo));
  103.             theError = ASoundPauseForAdjust (mySoundInfo);
  104.         }
  105.     }
  106. }
  107.  
  108. /*-----------------------------------------------------------------------*/
  109.         void    MyIdleProc (EventRecord *evt)
  110. /*-----------------------------------------------------------------------*/
  111. {
  112. #ifndef __SC__
  113. #pragma unused (evt)
  114. #endif
  115.     long        curBuf        = nil;
  116.  
  117.     if (soundPlaying == true) {
  118.         curBuf = ASoundGetCurBuffer (mySoundInfo);
  119.         if (curBuf != nil) {
  120.             SetControlValue (scrollBar, curBuf);
  121.         }
  122.     }
  123.  
  124.     if (ASoundIsDone (mySoundInfo) == true) {
  125.         EnableControl (startButton);
  126.         DisableControl (stopButton);
  127.         DisableControl (resumeButton);
  128.         DisableControl (scrollBar);
  129.         SetControlValue (scrollBar, kScrollMinValue);
  130.         ASoundDonePlaying (mySoundInfo, kCloseFile + kFreeMem);
  131.         InstallIdleProc ((EventProcs)nil);
  132.     }
  133. }
  134.  
  135. /*-----------------------------------------------------------------------*/
  136.         OSErr    DoPlay (void)
  137. /*-----------------------------------------------------------------------*/
  138. {
  139.     OSErr    theErr;
  140.     
  141.     theErr = ASoundGetFileToPlay (mySoundInfo);
  142.     if (theErr == noErr) {
  143.         theErr = ASoundStartPlaying (mySoundInfo, nil);
  144.         if (theErr == noErr) {
  145.             SADisableMe();
  146.             EnableControl(resumeButton);
  147.             EnableControl(stopButton);
  148.             paused = false;
  149.             InstallIdleProc ((EventProcs)MyIdleProc);
  150.             EnableControl (scrollBar);
  151.             SetControlMaximum (scrollBar, ASoundGetNumBuffers (mySoundInfo) - 1);
  152.         }
  153.     }
  154.  
  155.     return theErr;
  156. }
  157.  
  158. /*-----------------------------------------------------------------------*/
  159.         OSErr    DoPause (void)
  160. /*-----------------------------------------------------------------------*/
  161. {
  162.     Boolean            theError;
  163.     
  164.     theError = ASoundPause (mySoundInfo);
  165.  
  166.     if (paused == false) {
  167.         paused = true;
  168.         SetMyTitle ("\pResume Sound");
  169.     }
  170.     else {
  171.         paused = false;
  172.         SetMyTitle ("\pPause Sound");
  173.     }
  174.  
  175.     return theError;
  176. }
  177.  
  178. /*-----------------------------------------------------------------------*/
  179.         OSErr    DoStop (void)
  180. /*-----------------------------------------------------------------------*/
  181. {
  182.     OSErr theError;
  183.  
  184.     theError = ASoundStop (mySoundInfo);
  185.  
  186.     SADisableMe();
  187.     EnableControl(startButton);
  188.     DisableControl(resumeButton); 
  189.  
  190.     if (paused == true) {
  191.         SetControlTitle(resumeButton,"\pPause Sound");
  192.     }
  193.  
  194.     return theError;
  195. }
  196.  
  197. /*-----------------------------------------------------------------------*/
  198. pascal    short    DoPostGroupHit (long  modifiers,
  199.                                 ControlRef theControl,
  200.                                 ButtonItemRecHandle brh,
  201.                                 short item)
  202. /*-----------------------------------------------------------------------*/
  203. {
  204. #pragma unused (modifiers)
  205. #pragma unused (theControl)
  206. #pragma unused (brh)
  207.  
  208.     switch (item) {
  209.         case qtStyle:
  210.             DisableControl (checkBox);
  211.             SetControlValue (checkBox, false);
  212.             CDStyle = false;
  213.             break;
  214.         case cdStyle:
  215.             EnableControl (checkBox);
  216.             SetControlValue (checkBox, gPlayBackwards);
  217.             CDStyle = true;
  218.             break;
  219.         default:
  220.             DebugStr ("\pDoPostGroupHit got an invalid control passed to it!");
  221.     }
  222.  
  223.     return nil;
  224. }
  225.  
  226. /*-----------------------------------------------------------------------*/
  227.         short    DoPlayBackwards (long modifiers)
  228. /*-----------------------------------------------------------------------*/
  229. {
  230. #pragma unused (modifiers)
  231.  
  232.     gPlayBackwards = !gPlayBackwards;
  233.     SetControlValue (checkBox, gPlayBackwards);
  234.  
  235.     return nil;
  236. }
  237.  
  238. /*-----------------------------------------------------------------------*/
  239.         void    main (void)
  240. /*-----------------------------------------------------------------------*/
  241. {
  242.     Rect    r;
  243.     short    err;
  244.     Str255 names[] = {"\pQuickTime style",
  245.                       "\pCD style"};
  246.  
  247.     InitSimpleApp (2, kUseStandardMenu);          /* Simple App Sets up the Tool Box For us */
  248.     GetDocumentWindow (128);                     /* Get our stored window */
  249.  
  250.     SetRectLocation (&r, 10, 2);                /* This Sets a rects anchor point */
  251.     SetRectDimensions (&r, 90, 20);                /* This Sets its size without changing it position */
  252.     {
  253.         long buttonID = 1;
  254.         err = InstallPushButton (&buttonID, gSACurrentWindow, "\pPlay Sound", &r, (char)0, (ButtonHitProc) DoPlay, (ButtonUpdateProc)nil);
  255.         startButton = gSACurControl;
  256.     }
  257.     
  258.     SetRectLocation (&r, 110, 2);                /* This Sets a rects anchor point */
  259.     SetRectDimensions (&r, 100, 20);            /* This Sets its size without changing it position */
  260.     {
  261.         long buttonID = 2; 
  262.         err = InstallPushButton (&buttonID, gSACurrentWindow, "\pPause Sound", &r, (char)0, (ButtonHitProc) DoPause, (ButtonUpdateProc)nil);
  263.         resumeButton  = gSACurControl;
  264.         DisableControl (resumeButton);
  265.     }
  266.     
  267.     SetRectLocation (&r, 220, 2);                /* This Sets a rects anchor point */
  268.     SetRectDimensions (&r, 90, 20);                /* This Sets its size without changing it position */
  269.     {
  270.         long buttonID = 3;
  271.         err = InstallPushButton (&buttonID, gSACurrentWindow, "\pStop Sound", &r, (char)0, (ButtonHitProc) DoStop, (ButtonUpdateProc)nil);
  272.         stopButton = (ControlHandle)SAGetObjectHandle(buttonID);    /* lets remember the control handle */
  273.         DisableControl (stopButton);                    /* disable the control */
  274.     }
  275.  
  276.     SetRectDimensions (&r, 300, 16);
  277.     SetRectLocation (&r, 10, 30);
  278.     {
  279.      long scrollBarID  = 4;    
  280.         err = InstallScrollBar (&scrollBarID, gSACurrentWindow, "\p", &r, (char)0, (MyActionProc)DoScroll, (MyActionProc)nil, (MyActionProc)nil);
  281.         scrollBar = (ControlHandle)SAGetObjectHandle(scrollBarID);        /* get the object control handle */
  282.         DisableControl (scrollBar);
  283.         SetControlMinimum (scrollBar, kScrollMinValue);
  284.         SetControlMaximum (scrollBar, kScrollMinValue);
  285.         SetControlValue (scrollBar, kScrollMinValue);
  286.     }
  287.  
  288.     SetRectLocation (&r, 20, 100);
  289.     {
  290.         long checkBoxID = 5;
  291.         err = InstallCheckBox (&checkBoxID, gSACurrentWindow, "\pBackwards playing", &r, nil, false, (ButtonHitProc)DoPlayBackwards, (ButtonUpdateProc)nil);
  292.         checkBox = (ControlHandle)SAGetObjectHandle(checkBoxID);
  293.         DisableControl (checkBox);
  294.     }
  295.  
  296.     SetRectLocation (&r, 10, 60);
  297.     SetRectDimensions (&r, 130, 40);
  298.     err    = InstallRadioGroup (6,                    /* Group ID */
  299.                             gSACurrentWindow,     /* owner window */
  300.                             2,                    /* Number of items */
  301.                             "\pRadio Group",    /* Group Name */
  302.                             names,                 /* Array of button Names */
  303.                             &r,                 /* Group bounds */
  304.                             nil,                /* default item (zero based) */
  305.                             2,                     /* Horz spacing */
  306.                             2,                     /* vert spacing */
  307.                             16,                    /* Button Height */
  308.                             130,                /* Button widths */
  309.                             nil,                /* Pre Hit Proc */
  310.                             DoPostGroupHit,     /* Post Hit Proc */
  311.                             nil                    /* Group Update Proc */
  312.                             );
  313.  
  314.     GetPrintArea (gSACurrentWindow,&r);            /* Set the print area top coordinate */
  315.     r.top = 22;                                  /* so that you don't scroll the button out of view */
  316.     SetPrintArea (gSACurrentWindow,&r);
  317.     mySoundInfo = ASoundNew (&err);
  318.  
  319.     if (err == noErr)
  320.         Run ();                                 /* Let SimpleApp handle the rest */
  321. }
  322.  
  323. // utility functions since I already keep the control handle around
  324. /*-----------------------------------------------------------------------*/
  325.     void EnableControl (ControlRef cr)
  326. /*-----------------------------------------------------------------------*/
  327. {
  328.     if (cr != nil) {
  329.         HiliteControl(cr, nil);
  330.     }
  331. }
  332.  
  333. /*-----------------------------------------------------------------------*/
  334.     void DisableControl (ControlRef cr)
  335. /*-----------------------------------------------------------------------*/
  336. {
  337.     if (cr != nil) {
  338.         HiliteControl(cr, 255);
  339.     }
  340. }
  341.